

<?php 
if($_SESSION['Permit']==1)
{ 
   
	$sqlsection =sprintf("select * from tblsection ");
 	$sqlAllmenu =sprintf("select id,vi_name from tblmenu where parent_id='0'");// lay menu cap 1
   	$ListMenu=mysql_query($sqlAllmenu)or die(mysql_error());
    $sqlmenusection =sprintf("select id,vi_name from tblmenu where   section_id>=0"); //lay menu cap 2
} 
else // truong hop la bien tap vien
{ 
 	$sqlsection =sprintf("SELECT id,name FROM tblsection where id in(%s)",$_SESSION['ArraySec']);
	$sqlmenusection =sprintf("select id,vi_name from tblmenu where section_id in (%s)",$_SESSION['ArraySec']);

}  
  
if(isset($_POST['temp']))
{
 
  $sqlarticle =sprintf("select id,vi_title from tblarticle where section_id=%s",$_POST['temp']);
  $Article=mysql_query($sqlarticle)or die(mysql_error()); 
 
}
$sqlmenu =sprintf("select * from tblmenu where id='%s'",$_GET['id']);
$Menu=mysql_query($sqlmenu)or die(mysql_error());

$MenuSection=mysql_query($sqlmenusection)or die(mysql_error());
$Section=mysql_query($sqlsection)or die(mysql_error()); 
  
   
?>  
  <script type="text/javascript">
  function btnAdd_Click(){
	var frm = document.detailt;
	var menus= frm.menus;
	var level;
	var valid = true;
	for(var i = 0; i<menus.length;i++){
		
		if(menus[i].checked){
				level = menus[i].value;
		}
	}
	
	var sid = frm.sid.value;
	var aid = frm.aid.value;
	var vi_name = frm.vi_name.value;
	var en_name = frm.en_name.value;
	
	
	if(level =="section" && sid=="0"){
		alert("Menu cấp 2 phải được liên kết đến 1 chuyên mục!");
		valid = false;
	}
	if(level =="article" && (aid =="0" || sid=="0")){
		alert("Menu cấp 3 phải được liên kết đến 1 bài viết!");
		valid = false;
	}
	if(vi_name==""){
		alert("Thiếu tên tiếng Việt!");
		valid=false;
	}
	if(en_name==""){
		alert("Thiếu tên tiếng Anh!");
		valid=false;
	}
	if(valid){
		frm.submit();
		
	}

}

function test(control)
  {	  
   
    
	   if(document.getElementById('parent_id').value=="")
	   {
	   alert('Hãy chọn chuyên mục!');
	   return 0;
	   }
	   document.detailt.submit();
  }

  function submitt(control)
  {	  
   
    document.getElementById('temp').value=control.value;
	
	document.formEdit.submit();
  }
  function changevalue(control)
  {	  
  	if(control.value!=0) document.getElementById('url').value="view/"+control.value;
  }
  </script>
  
<form name="formEdit" action="" method="POST" style="visibility:hidden">
	<input type="hidden" name="temp" id="temp" value=""/>
</form>

<div style="font-size:16px;"> 
<?php if(isset($_GET['id'])) $row_CurrentLink=mysql_fetch_assoc($Menu);?>
  <fieldset style="width:90%;">
       
  	<legend>Thông tin chi tiết của menu</legend>
  	<form name="detailt" action="Menu/savemenu.php" method="POST">
      <fieldset style="width:90%;">
       
  	<legend>Lựa chọn bài viết</legend>
     <p id="field"><span  id="text">Chuyên mục</span>
      <select name="sid" onchange="submitt(this)" >
      	<option value="0">Chọn một chuyên mục</option>
        <?php 
			
			
			$sid=$row_CurrentLink["section_id"];
			while ($rowitem=mysql_fetch_assoc($Section)){  
		?>
        		<option value="<?php echo $rowitem['id']?>" <?php if($rowitem['id']==$sid) echo "selected='selected'"?> <?php if(isset($_POST['temp'])&&$_POST['temp']==$rowitem['id']) echo "selected='selected'"; ?>  >
				<?php echo $rowitem['name']?></option>
        <?php		
} 

?>     </select></p>


		<?php
		
			
			
		?>
    <p id="field"><span  id="text" >Liên kết tới bài viết</span>
    
      <select name="aid" id="link" onchange="changevalue(this)" onfocus="changevalue(this)" >
      	<option value="0">Lựa chọn bài viết</option>
        <?php 
			$parts = explode("/",$row_CurrentLink["URL"]);
			$aid= $parts[2];
			
			while ($rowitem=mysql_fetch_assoc($Article)){  
		?>
        <option value="<?php echo $rowitem['id']?>" <?php if($rowitem['id']==$aid) echo "selected='selected'"?> > <?php echo $rowitem['vi_title']?></option>
        <?php
	
} 
 
?>     </select>
    </p>
    </fieldset>
     
      <input name="link_url" id="url" type="hidden" value="<?php echo  $row_CurrentLink['URL']?>"/>
      
      <p style=" <?php if($_SESSION['Permit']!=1) echo "display:none" ?>">
      	<span id="text"> Chọn Cấp Menu	</span>
        <label for="menus-none">
        	<input  id="menus-none" type="radio" name="menus" value="menu" onclick="disableall();"  <?php if($row_CurrentLink['section_id']==0) echo "checked='checked'"; ?> />
            Menu
        </label> 
      <label for="menus-all"><input id="menus-all" <?php if($row_CurrentLink['section_id']>0) echo "checked='checked'"; ?> type="radio" name="menus" value="section" onclick="enableselections();" />Section</label> 
							
							<label for="menus-select"><input id="menus-select"  <?php if($row_CurrentLink['section_id']<0) echo "checked='checked'"; ?> type="radio" name="menus" value="article" onclick="enablesection();" />Article</label>
      </p>
           <p id="field"><span id="text">Lựa chọn Menu Cha</span>
           
            
             <select style=" <?php if($_SESSION['Permit']!=1) echo "display:none" ?>" name="parent_id" id="parent_id" size="6" >
                     <?php 
							
						while ($rowitem=mysql_fetch_assoc($ListMenu)){  
						?>
								<option <?php if($row_CurrentLink['parent_id']==$rowitem['id']) echo "selected='selected'"  ;?>  value="<?php echo $rowitem['id']?>"><?php echo $rowitem['vi_name']?></option>
								<?php		
						} 
						 
						?> 
             </select>
             <select  name="section_id" id="section_id" size="6" >
               <?php 
								
						while ($rowitem=mysql_fetch_assoc($MenuSection)){  
						?>
               <option <?php if($row_CurrentLink['parent_id']==$rowitem['id']) echo "selected='selected'"  ;?> value="<?php echo $rowitem['id']?>"><?php echo $rowitem['vi_name']?></option>
               <?php		
						} 
						 
						?>
             </select>
           </p>
   
   
    
        
       <input name="menu_id" type="hidden" value="<?php if(isset($_GET['id'])) echo $row_CurrentLink['id']; ?>" />

     
     <p id="field"><span id="text">Tên Tiếng Việt </span> 
         <input name="vi_name" type="text"  value="<?php if(isset($_GET['id'])) echo $row_CurrentLink['vi_name']; ?>" size="50" />
      </p>
        <p id="field"><span id="text">Tên Tiếng Anh </span> 
          <input name="en_name" type="text"  value="<?php if(isset($_GET['id'])) echo $row_CurrentLink['en_name']; ?>" size="50" />
        </p>
    
    <p id="field"><span id="text">Sắp xếp</span> 
          <input name="order" type="text"  value="<?php if(isset($_GET['id'])) echo $row_CurrentLink['order']; ?>" size="50" />
        </p>
     
   
   
    <p align="center" id="field">    
       
         <input type="submit" name="click" value="Lưu lại"/>
       <input type ="reset" value="Làm lại"/>
      </p>
  </form>
</fieldset>
<script type="text/javascript"> 
				 
				<?php  if($row_CurrentLink['section_id']>0) echo "enableselections()";
				else if($row_CurrentLink['section_id']<0) echo "enablesection()";
				else echo "disableall()";
				 ?>
				  
				  function disableall() {
					  disableselections();
					  disablesection();
				  }
					function disableselections() {
						var e = document.getElementById('parent_id');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = false;
						}
					}
					function disablesection() {
						var e = document.getElementById('section_id');
							e.disabled = true;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = true;
							e.options[i].selected = false;
						}
					}
					function enableselections() {
						var e = document.getElementById('parent_id');
						disablesection();
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
						
					}
					function enablesection() {
						var e = document.getElementById('section_id');
						 disableselections();
							e.disabled = false;
						var i = 0;
						var n = e.options.length;
						for (i = 0; i < n; i++) {
							e.options[i].disabled = false;
						}
						
					}
				</script> 
</div>